package org.sgroup.imenu;

import java.util.Random;
import org.sgroup.imenu.bo.Item;
import org.sgroup.imenu.util.CData;
import org.sgroup.imenu.util.LoadBitmap;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DetailItemView extends RelativeLayout {
	private Context mContext;
	
	public DetailItemView(Context context, Item item)
	{		
		super(context);
		mContext = context;
		
		LayoutInflater linflater = (LayoutInflater) ((Activity) mContext).getSystemService(Service.LAYOUT_INFLATER_SERVICE);
		linflater.inflate(R.layout.detail_item, this);
		
		ImageButton btnThumbnail = (ImageButton) findViewById(R.id.item_thumbnail);
		TextView tvItemName = (TextView) findViewById(R.id.item_name);
		TextView tvItemDesc = (TextView) findViewById(R.id.item_desc);
		TextView tvItemPrice = (TextView) findViewById(R.id.item_price);
		TextView tvItemTime = (TextView) findViewById(R.id.item_time);
		Button btnAddCart = (Button) findViewById(R.id.btnAddCart);					

		// get current item
		final Item currentItem = item;

		tvItemName.setText(currentItem.Name);
		tvItemDesc.setText(currentItem.Description);
		tvItemPrice.setText(CData.FormatPrice(currentItem.Price));
//		Random randTime = new Random();		
//		tvItemTime.setText(String.valueOf(15 + randTime.nextInt(30)));
		tvItemTime.setText(String.valueOf(currentItem.Time));
		if (!currentItem.ImageFile.isEmpty()) {
			try {
				Bitmap bm= LoadBitmap.GetBitmap(mContext,currentItem.ImageFile);
				btnThumbnail.setImageBitmap(bm);
			} catch (Exception e) {
				Log.v("ItemListAdapter", e.getMessage());
			}
		}		
		
		// click button btnAddCart
		btnAddCart.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				((IMenuActivity)mContext).AddOrder(currentItem);				
			}
		});
	}
}
